<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="120px">
      <el-form-item label="菜品标题">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item label="菜品描述">
        <el-input v-model="form.description" />
      </el-form-item>
      <el-form-item label="菜品价格">
        <el-input v-model="form.price" />
      </el-form-item>
      <el-form-item label="菜系">
        <el-select v-model="form.cuisineId">
          <el-option
            v-for="item in cuisineList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="封面">
        <el-upload
          class="upload-demo"
          action="/api-v1/upload"
          :on-success="uploadSuccess"
          :file-list="form.cover?[{ url: form.cover }]:[     ]"
          list-type="picture"
          :limit="1"
        >
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存信息</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getMenu, saveMenu } from '@/api/api-v1/menu';
import { cuisineList } from '@/api/api-v1/cuisine';
export default {
  data() {
    return {
      form: {
        state: false
      },
      cuisineList: []
    };
  },
  methods: {
    onSubmit() {
      saveMenu(this.form).then(() => {
        this.$message.success('保存信息成功');
      });
    },
    onCancel() {
      this.$message({
        message: 'cancel!',
        type: 'warning'
      });
    },
    uploadSuccess(res) {
      this.form.cover = res.data;
    }
  },
  created() {
    if (this.$route.query.id) {
      getMenu(this.$route.query.id).then(res => {
        this.form = res.data;
      });
    }
    cuisineList().then(res => {
      this.cuisineList = res.data;
    });
  }
};
</script>

<style scoped>
.line {
  text-align: center;
}
</style>
